package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.LoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class LoginUserController {
    @Autowired
    private LoginUserService loginUserService;
        //用户登录提交方法
        @RequestMapping("/loginpage")
        public String login(){
            return "login";//登录页面
    }
    /*
    * 验证用户身份
    * */
    @RequestMapping(value="/loginYanZhen",method = RequestMethod.POST)
    /*@RequestMapping("/loginYanZhen")*/
    public String loginYanZhen(HttpSession session, String username, String password) throws Exception{

        if (username.equals("admin")&&password.equals("123")){
            return "admin";//去后台
        }
        User user = loginUserService.loginYanZhen(username, password);
        if(user!=null){
            //说明用户名和密码正确
            session.setAttribute("user",user);

            User user1 = (User) session.getAttribute("user");
            System.out.println(user1);
            return "login";//重定向到首页
        }else{
        return "login";//重定向到登录页面
    }
}
    //注册
    @ResponseBody
    @RequestMapping(value = "/zhuce",method = RequestMethod.POST)
    public Integer zhuce(User user){
        System.out.println(user);
        User userByName = loginUserService.findUserByName(user.getUsername());
        if(userByName!=null){
          return 0;//用户名存在
        }
        /*if(userByName.getUsername()!=null){
        }*/
        int i = loginUserService.zhuCe(user);
        return i;//跳转到登陆页

    }
   /* *//*
 * //注册用户
@RequestMapping(value = "/zhuce",method = RequestMethod.POST)
@ResponseBody
public String user(User user){
User byName = userService.findByName(user.getUsername());
if (byName!=null){
 return  null;
}
userService.addUser(user);
return "index";//跳转到登录页面
}*/
    //切换用户、注销
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //session失效
        Object user = session.getAttribute("user");
        System.out.println(user+"前===================================================");
        session.invalidate();
        //重定向到登录页面


        return "redirect:/login/loginpage";
    }
    @RequestMapping("/tui")
    public String  tuichu(){
        return "login";
    }
}
